Skip to content

Fix custom field serializer inside a flattened field#756

Open
gaoflow wants to merge 1 commit into
yukinarit:mainfrom
gaoflow:fix/453-flatten-custom-serializer
Open

Fix custom field serializer inside a flattened field#756
gaoflow wants to merge 1 commit into
yukinarit:mainfrom
gaoflow:fix/453-flatten-custom-serializer

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 7, 2026

Copy link
Copy Markdown

Fixes #453.

A custom field serializer on a field of a flattened dataclass crashed serialization:

@serialize
class Child:
    value: int = field(serializer=str)

@serialize
class Parent:
    child: Child = field(flatten=True)

to_dict(Parent(child=Child(value=3)))  # SerdeError: name 'value_serializer' is not defined

Cause

A flattened dataclass field is rendered inline in the enclosing class's generated serialize function, so it emits references like value_serializer(...). But the loop that registers custom field serializers (and skip_if predicates) in the generated globals only iterated the top-level fields — the flattened child's serializer was never put in scope.

Fix

Recurse into flattened (and optional-flattened) dataclass fields when collecting field serializers / skip_if predicates, so they are available in the enclosing scope. Handles nested flattens too.

Tests

Added test_flatten_custom_field_serializer (basic + nested flatten; fails without the patch). Full suite green (5169 passed). Deserialization was already unaffected (a flattened child is deserialized via its own scoped function).

Thanks @AustinScola for the report and minimal reproducer.

A flattened dataclass field is rendered inline in the enclosing class's
serialize function, but only the top-level fields' custom serializers
were registered in the generated globals. Referencing a flattened
child's field serializer therefore raised
`SerdeError: name '<field>_serializer' is not defined`.

Recurse into flattened (and optional-flattened) dataclass fields when
collecting field serializers and skip_if predicates for the scope.

Fixes yukinarit#453
@codecov

codecov Bot commented Jun 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.98%. Comparing base (f09d503) to head (5f62546).
⚠️ Report is 75 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #756      +/-   ##
==========================================
+ Coverage   90.83%   90.98%   +0.15%     
==========================================
  Files          13       13              
  Lines        2040     2352     +312     
  Branches      370      462      +92     
==========================================
+ Hits         1853     2140     +287     
- Misses        124      140      +16     
- Partials       63       72       +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom field serialzer in flattened field bug

1 participant